﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MobilePlatform.IFactory;
using Asd.Data;
using Asd.MP.Data;
using Asd.MP.DomainModel;
using Microsoft.Practices.Unity;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Unity;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Asd.MP.Entity;

namespace Asd.MP.DataAccess
{
    public class SystemInfoDataAccess : ISystemInfoDataAccess
    {
        [Microsoft.Practices.Unity.Dependency]
        public IDataContext IDataContext { get; set; }

        public SystemInfoDataAccess()
        {
            DIContainer.Default.BuildUp(this);
        }


        public V_SerialNoListDataSet.V_SERIALNOLISTDataTable GetSerailNoList()
        {
            using (this.IDataContext)
            {
                return this.IDataContext.GetData<V_SerialNoListDataSet.V_SERIALNOLISTDataTable>();
            }
        }


        public V_SerialNoListDataSet.V_SERIALNOLISTDataTable GetSerailNoBySn(string sn)
        {
            using (this.IDataContext)
            {
                return this.IDataContext.GetDataBy<V_SerialNoListDataSet.V_SERIALNOLISTDataTable>(sn);

            }
        }

        public void SaveSerail(SerialNoEntity serial)
        {
            using (this.IDataContext)
            {
                var serialDs = new Tb_Core_SerialNoDataSet();
                var serialDt = this.IDataContext.GetDataBy<Tb_Core_SerialNoDataSet.TB_CORE_SNDataTable>(serial.Sn);
                var oneRow = serialDt.FirstOrDefault();
                bool isNew = oneRow == null;
                if (isNew)
                    oneRow = serialDt.NewTB_CORE_SNRow();

                oneRow.EMPACCOUNT = serial.EMPAccount;
                oneRow.MOBILE = serial.Mobile;
                oneRow.REMARK = serial.Remark;
                oneRow.SN = serial.Sn;
                oneRow.STATUS = serial.Status;

                if (isNew)
                    serialDt.AddTB_CORE_SNRow(oneRow);

                serialDs.Merge(serialDt);

                this.IDataContext.Save(serialDs);

            }
        }


        public V_SerialNoListDataSet.V_SERIALNOLISTDataTable GetSerailNoListByAccount(string account)
        {
            using (this.IDataContext)
            {
                return this.IDataContext.GetDataByCustom<V_SerialNoListDataSet.V_SERIALNOLISTDataTable>("GetDataByAccount", account);
            }
        }

        public V_SerialNoListDataSet.V_SERIALNOLISTDataTable GetSerailNoListByMobile(string mobile)
        {
            using (this.IDataContext)
            {
                return this.IDataContext.GetDataByCustom<V_SerialNoListDataSet.V_SERIALNOLISTDataTable>("GetDataByMobile", mobile);
            }
        }

        public void ModifySerialNoStatus(List<SerialNoEntity> serialList)
        {
            using (this.IDataContext)
            {
                var serialDs = new Tb_Core_SerialNoDataSet();
                var serialDt = this.IDataContext.GetData<Tb_Core_SerialNoDataSet.TB_CORE_SNDataTable>();

                serialDt.Join(serialList, a => a.SN, b => b.Sn, (a, b) =>
                {
                    a.STATUS = b.Status;
                    return false;
                }).ToList();

                serialDs.Merge(serialDt);
                this.IDataContext.Save(serialDs);
            }
        }


    }


}
